package cn.itcast.controller.company;

import cn.itcast.controller.BaseContorller;
import cn.itcast.domain.company.Company;

import cn.itcast.service.CompanyService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.UUID;

@Controller
@RequestMapping("/company")
public class CompanyController extends BaseContorller {

    @Reference  //从远程获得,用dubbo的注解
    private CompanyService companyService;


    @RequestMapping(value = "/list",name = "分页展示企业数据显示")
    public String findAll(@RequestParam(value = "page",defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "5") Integer pageSize){
        PageInfo pageInfo =companyService.findPage(pageNum,pageSize);
        request.setAttribute("page",pageInfo);
        return "company/company-list";
    }

    @RequestMapping(value = "/toAdd",name = "跳转添加企业")
    public String toAdd(){

        return "company/company-add";
    }


    @RequestMapping(value = "/edit",name = "保存企业")
    public String edit(Company company){

        if (StringUtils.isEmpty(company.getId())){//id为空
            company.setId(UUID.randomUUID().toString());
            companyService.save(company);

        }else {//id不为空
            companyService.update(company);
        }


        return "redirect:/company/list.do";
    }

    @RequestMapping(value = "/toUpdate",name = "进入企业修改页面")
    public String update(String id){

        Company company = companyService.findById(id);
        request.setAttribute("company",company);
        return "company/company-add";
    }

    @RequestMapping(value = "/delete",name = "进入企业修改页面")
    public String delete(String id){

        companyService.deleteById(id);

        return "redirect:/company/list.do";
    }


}
